package com.example.tutorialspringbootrabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AmqpConfig {

    //
    public static final String EXCHANGE = "springboot.exchange";
    public static final String QUEUE = "springboot.queue";
    public static final String ROUTINGKEY = "springboot.routingkey";


    public static final String TOPIC_EXCHANGE = "springboot.topic.exchange";
    public static final String TOPIC_QUEUE = "springboot.topic.queue";
    public static final String TOPIC_ROUTINGKEY = "*.topic.routingkey.#";


    /**
     * 声明一个交换机
     *
     * @return
     */
    @Bean
    public DirectExchange exchange() {
        return ExchangeBuilder.directExchange(EXCHANGE).durable(true).build();
    }

    /**
     * 声明一个队列
     *
     * @return
     */
    @Bean
    public Queue queue() {
        return QueueBuilder.durable(QUEUE).build();
    }

    /**
     * 把交换机和队列，通过路由key进行绑定
     *
     * @return
     */
    @Bean
    public Binding binding(Queue queue, DirectExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(ROUTINGKEY);
    }


    //--------------------------------------------------------------------


    /**
     * 声明一个交换机
     *
     * @return
     */
    @Bean
    public TopicExchange topicExchange() {
        return ExchangeBuilder.topicExchange(TOPIC_EXCHANGE).durable(true).build();
    }

    /**
     * 声明一个队列
     *
     * @return
     */
    @Bean
    public Queue topicQueue() {
        return QueueBuilder.durable(TOPIC_QUEUE).build();
    }

    /**
     * 把交换机和队列，通过路由key进行绑定
     *
     * @return
     */
    @Bean
    public Binding topicBinding(Queue topicQueue, TopicExchange topicExchange) {
        return BindingBuilder.bind(topicQueue).to(topicExchange).with(TOPIC_ROUTINGKEY);
    }


    //--------------------------------------------------------------------
    //一个交换机绑定了多个队列

    public static final String EXCHANGE_X = "springboot.exchange.x";
    public static final String QUEUE_01 = "springboot.queue.01";
    public static final String QUEUE_02 = "springboot.queue.02";
    public static final String QUEUE_03 = "springboot.queue.03";
    public static final String ROUTINGKEY_X = "springboot.routingkey.x";

    @Bean
    public DirectExchange exchangeX() {
        return ExchangeBuilder.directExchange(EXCHANGE_X).durable(true).build();
    }
    @Bean
    public Queue queue01() {
        return QueueBuilder.durable(QUEUE_01).build();
    }
    @Bean
    public Queue queue02() {
        return QueueBuilder.durable(QUEUE_02).build();
    }
    @Bean
    public Queue queue03() {
        return QueueBuilder.durable(QUEUE_03).build();
    }
    @Bean
    public Binding bindingX01(Queue queue01, DirectExchange exchangeX) {
        return BindingBuilder.bind(queue01).to(exchangeX).with(ROUTINGKEY_X);
    }
    @Bean
    public Binding bindingX02(Queue queue02, DirectExchange exchangeX) {
        return BindingBuilder.bind(queue02).to(exchangeX).with(ROUTINGKEY_X);
    }
    @Bean
    public Binding bindingX03(Queue queue03, DirectExchange exchangeX) {
        return BindingBuilder.bind(queue03).to(exchangeX).with(ROUTINGKEY_X);
    }

}